import java.util.List;
import java.util.ArrayList;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-09-05
 * Time: 18:58
 */
public class Test {
    int[] tmp1;
    //leetcode 912 排序数组——归并排序
    public int[] sortArray(int[] nums) {
        int n = nums.length;
        tmp = new int[n];
        mergesort(nums,0,n-1);
        return nums;
    }

    private void mergesort(int[] nums, int left, int right) {
        if(left >= right) return;
        int mid = left + (right - left) / 2;

        mergesort(nums,left,mid);
        mergesort(nums,mid + 1,right);

        int cur1 = left,cur2 = mid + 1,i = 0;
        while(cur1 <= mid && cur2 <= right) {
            tmp[i++] = nums[cur1] < nums[cur2] ? nums[cur1++] : nums[cur2++];
        }

        while(cur1 <= mid) tmp[i++] = nums[cur1++];
        while(cur2 <= right) tmp[i++] = nums[cur2++];

        for(int j = left; j <= right; j++) nums[j] = tmp[j - left];
    }


    //剑指offer 51 数组中的逆序对
    public int reversePairs1(int[] nums) {
        int n = nums.length;
        tmp = new int[n];
        return mergeSort1(nums,0,n-1);
    }

    private int mergeSort1(int[] nums,int left, int right) {
        if(left >= right) return 0;
        int ret = 0;
        int mid = left + (right - left) / 2;
        ret += mergeSort(nums,left,mid);
        ret += mergeSort(nums,mid + 1,right);
        int cur1 = left, cur2 = mid + 1,i = 0;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur1] > nums[cur2]) {
                ret += mid - cur1 + 1;
                tmp[i++] = nums[cur2++];
            }else {
                tmp[i++] = nums[cur1++];
            }
        }

        while(cur1 <= mid) tmp[i++] = nums[cur1++];
        while(cur2 <= right) tmp[i++] = nums[cur2++];

        for(int j = left; j <= right; j++) nums[j] = tmp[j - left];

        return ret;
    }

    //leetcode 315 计算右侧小于当前元素的个数
    int[] ret;
    int[] tmpNums;
    int[] index;
    int[] tmpIndex;
    public List<Integer> countSmaller(int[] nums) {
        int n = nums.length;
        ret = new int[n];
        tmpNums = new int[n];
        index = new int[n];
        for(int i = 0; i < n; i++) index[i] = i;
        tmpIndex = new int[n];
        mergeSort(nums,0,n-1);
        List<Integer> list = new ArrayList<>();
        for(int x : ret) list.add(x);
        return list;
    }

    private void mergeSort2(int[] nums, int left, int right) {
        if(left >= right) return;
        int mid = left + (right - left) / 2;
        mergeSort2(nums,left,mid);
        mergeSort2(nums,mid + 1,right);
        int cur1 = left,cur2 = mid + 1,i = 0;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur1] <= nums[cur2]) {
                tmpNums[i] = nums[cur2];
                tmpIndex[i++] = index[cur2++];
            }else {
                ret[index[cur1]] += right - cur2 + 1;
                tmpNums[i] = nums[cur1];
                tmpIndex[i++] = index[cur1++];
            }
        }

        while(cur1 <= mid) {
            tmpNums[i] = nums[cur1];
            tmpIndex[i++] = index[cur1++];
        }
        while(cur2 <= right) {
            tmpNums[i] = nums[cur2];
            tmpIndex[i++] = index[cur2++];
        }

        for(int j = left; j <= right; j++) {
            nums[j] = tmpNums[j - left];
            index[j] = tmpIndex[j - left];
        }
    }

    //因为这里比较的是 nums[i] 与 2*nums[j] 的大小关系，所以不能直接判断nums[i] 与 nums[j] 的大小关系
    //所以这里我们需要先判断翻转对之后，然后才能对数组进行归并排序
    int[] tmp;
    public int reversePairs(int[] nums) {
        int n = nums.length;
        tmp = new int[n];
        return mergeSort(nums,0,n-1);
    }

    private int mergeSort(int[] nums, int left, int right) {
        if(left >= right) return 0;
        int mid = left + (right - left) / 2;
        int ret = 0;
        ret += mergeSort(nums,left,mid);
        ret += mergeSort(nums,mid + 1,right);
        int cur1 = left, cur2 = mid + 1,i = 0;
        //以升序为例
        while(cur2 <= right) {
            while(cur1 <= mid && (nums[cur1] / 2.0 <= nums[cur2])) cur1++;
            if(cur1 > mid) break;
            ret += mid - cur1 + 1;
            cur2++;
        }

        cur1 = left;
        cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right) {
            tmp[i++] = nums[cur1] <= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }

        while(cur1 <= mid) tmp[i++] = nums[cur1++];
        while(cur2 <= right) tmp[i++] = nums[cur2++];

        for(int j = left; j <= right; j++) nums[j] = tmp[j - left];

        return ret;
    }
}
